package geektime.spring.springbucks.customerservice;

import geektime.spring.springbucks.customerservice.support.CustomConnectionKeepAliveStrategy;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.boot.Banner;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.hateoas.hal.Jackson2HalModule;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class CustomerServiceApplication {

  public static void main(String[] args) {
    new SpringApplicationBuilder()
        .sources(CustomerServiceApplication.class)
        .bannerMode(Banner.Mode.OFF)
        .web(WebApplicationType.NONE)
        .run(args);
  }

  @Bean
  public Jackson2HalModule jackson2HalModule() {
    return new Jackson2HalModule();
  }

  @Bean
  public HttpComponentsClientHttpRequestFactory requestFactory() {
    PoolingHttpClientConnectionManager connectionManager =
        new PoolingHttpClientConnectionManager(30, TimeUnit.SECONDS);
    connectionManager.setMaxTotal(200);
    connectionManager.setDefaultMaxPerRoute(20);

    CloseableHttpClient httpClient = HttpClients.custom()
        .setConnectionManager(connectionManager)
        .evictIdleConnections(30, TimeUnit.SECONDS)
        .disableAutomaticRetries()
        // 有 Keep-Alive 认里面的值，没有的话永久有效
        //.setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)
        // 换成自定义的
        .setKeepAliveStrategy(new CustomConnectionKeepAliveStrategy())
        .build();

    HttpComponentsClientHttpRequestFactory requestFactory =
        new HttpComponentsClientHttpRequestFactory(httpClient);

    return requestFactory;
  }

  @Bean
  public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder
        .setConnectTimeout(Duration.ofMillis(100))
        .setReadTimeout(Duration.ofMillis(500))
        .requestFactory(this::requestFactory)
        .build();
  }

}
